Kotlin

Null Safety

Swift

Nullable types and Non-Null Types

                  var a: String = "abc" // Regular initialization means non-null by default
a = null // compilation error
                
                    var a: String = "abc" // Regular initialization means non-null by default
a = null // compilation error
                  
                  var b: String? = "abc" // can be set null
b = null // ok
print(b)
                
                    var b: String? = "abc" // can be set null
b = null // ok
print(b)
                  
                  val l = a.length
                
                    let l = a.length
                  
                  val l = b.length // error: variable 'b' can be null
                
                    let l = b.length // error: variable 'b' can be null
                  

Checking for null in conditions

                  val l = if (b != null) b.length else -1
                
                    let l = if (b != null) b.length else -1
                  
                  val b: String? = "Kotlin"
if (b != null && b.length > 0) {
    print("String of length ${b.length}")
} else {
    print("Empty string")
}
                
                    let b: String? = "Kotlin"
if (b != null && b.length > 0) {
    print("String of length ${b.length}")
} else {
    print("Empty string")
}
                  

Safe Calls

                  val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
                
                    let a = "Kotlin"
let b: String? = null
print(b?.length)
print(a?.length) // Unnecessary safe call
                  
                  bob?.department?.head?.name
                
                    bob?.department?.head?.name
                  
                  val listWithNulls: List<String?> = listOf("Kotlin", null)
for (item in listWithNulls) {
    item?.let { println(it) } // prints Kotlin and ignores null
}
                
                    let listWithNulls: List<String?> = listOf("Kotlin", null)
for (item in listWithNulls) {
    item?.let { print(it) } // prints Kotlin and ignores null
}
                  
                  // If either `person` or `person.department` is null, the function is not called:
person?.department?.head = managersPool.getManager()
                
                    // If either `person` or `person.department` is null, the function is not called:
person?.department?.head = managersPool.getManager()
                  

Elvis Operator

                  val l: Int = if (b != null) b.length else -1
                
                    let l: Int = if (b != null) b.length else -1
                  
                  val l = b?.length ?: -1
                
                    let l = b?.length ?? -1
                  
                  fun foo(node: Node): String? {
    val parent = node.getParent() ?: return null
    val name = node.getName() ?: throw IllegalArgumentException("name expected")
    // ...
}
                
                    func foo(node: Node) -> String? {
    let parent = node.getParent() ?? return null
    let name = node.getName() ?? throw IllegalArgumentException("name expected")
    // ...
}
                  

The !! Operator

                  val l = b!!.length
                
                    let l = b!!.length
                  

Safe Casts

                  val aInt: Int? = a as? Int
                
                    let aInt: Int? = a as? Int
                  

Collections of Nullable Type

                  val nullableList: List<Int?> = listOf(1, 2, null, 4)
val intList: List<Int> = nullableList.filterNotNull()
                
                    let nullableList: List<Int?> = listOf(1, 2, null, 4)
let intList: List<Int> = nullableList.filterNotNull()